package chen.web.controller.forum.form;

import java.util.Date;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

import chen.web.controller.AdminRoleVerify;
import chen.web.controller.UbbParserFactory;
import chen.web.util.ContentFilter;
import chen.web.util.ubb.UbbParser;
import chen.web.widget.Widget;
import chen.web.widget.vote.Vote;

public class SubjectForm {
	
	private int categoryId;
	@NotEmpty
	private String title;
	@NotEmpty
	@Size(min=3)
	private String content;
	private VoteForm vote;

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = ContentFilter.escapeXml(title);
	}
	public String getContent() {	
		return content;
	}
	public void setContent(String content) {
		UbbParser parser = AdminRoleVerify.isAdmin() ? UbbParserFactory.standard():
							UbbParserFactory.safe();
		this.content = parser.parse(content).toUbb(true);
	}
	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}
	public int getCategoryId() {
		return categoryId;
	}
	public void setVote(VoteForm vote) {
		this.vote = vote;
	}
	public VoteForm getVote() {
		return vote;
	}
	
	public Widget getWidget(){
		Widget widget = null;
		if(vote != null){
			Vote vote = new Vote();
			vote.setTitle(getTitle());
			vote.setDateline(new Date());
			long deadline = vote.getDateline().getTime()+getVote().getDays()*24*3600*1000l;
			vote.setDeadline(new Date(deadline));
			vote.setOptionalQuantity(getVote().getOptionalQuantity());
			vote.setPublicVoters(getVote().isPublicVoters());
			vote.setOptionsContent(getVote().getOptions());
			widget = vote;
		}
		return widget;
	}
}
